package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallBrand.Column;
import org.linlinjava.litemall.db.mapper.LitemallBrandDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallBrandService {
    @Resource
    private LitemallBrandDao brandDao;
    private Column[] columns = new Column[]{Column.id, Column.name, Column.desc, Column.picUrl, Column.floorPrice};

    public List<LitemallBrand> query(Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallBrand> query = brandDao.createLambdaQuery();
    	
    	query.andEq(LitemallBrand::getDeleted, Boolean.FALSE);
    	
    	if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
    		query.orderBy(sort + " " + order);
    	}
    	query.limit(page, limit);
    	List<LitemallBrand> list = query.select();
    	return list;
		/*
		 * LitemallBrandExample example = new LitemallBrandExample();
		 * example.or().andDeletedEqualTo(false); if (!StringUtils.isEmpty(sort) &&
		 * !StringUtils.isEmpty(order)) { example.setOrderByClause(sort + " " + order);
		 * } PageHelper.startPage(page, limit); return
		 * brandMapper.selectByExampleSelective(example, columns);
		 */
    }

    public List<LitemallBrand> query(Integer page, Integer limit) {
        return query(page, limit, null, null);
    }

    public LitemallBrand findById(Integer id) {
    	return brandDao.single(id);
        //return brandMapper.selectByPrimaryKey(id);
    }

    public List<LitemallBrand> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallBrand> query = brandDao.createLambdaQuery();
    	if (!StringUtils.isEmpty(id)) {
    		query.andEq(LitemallBrand::getId, id);
        }
        if (!StringUtils.isEmpty(name)) {
        	query.andLike(LitemallBrand::getName, "%" + name + "%");
        }
        query.andEq(LitemallBrand::getDeleted, Boolean.FALSE);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        
        query.limit(page, size);
    	List<LitemallBrand> list = query.select();
    	return list;
    	
		/*
		 * LitemallBrandExample example = new LitemallBrandExample();
		 * LitemallBrandExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(id)) { criteria.andIdEqualTo(Integer.valueOf(id)); }
		 * if (!StringUtils.isEmpty(name)) { criteria.andNameLike("%" + name + "%"); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * brandMapper.selectByExample(example);
		 */
    }

    public int updateById(LitemallBrand brand) {
    	
        brand.setUpdateTime(LocalDateTime.now());
        return  brandDao.updateTemplateById(brand);
       // return brandMapper.updateByPrimaryKeySelective(brand);
    }

    public void deleteById(Integer id) {
    	LitemallBrand brand = new LitemallBrand();
    	brand.setId(id);
    	brand.setDeleted(true);
    	brandDao.updateTemplateById(brand);
        //brandMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallBrand brand) {
        brand.setAddTime(LocalDateTime.now());
        brand.setUpdateTime(LocalDateTime.now());
        brandDao.insert(brand);
        //brandMapper.insertSelective(brand);
    }

    public List<LitemallBrand> all() {
    	LitemallBrand brand = new LitemallBrand();
    	brand.setDeleted(true);
    	return brandDao.template(brand);
		/*
		 * LitemallBrandExample example = new LitemallBrandExample();
		 * example.or().andDeletedEqualTo(false); return
		 * brandMapper.selectByExample(example);
		 */
    }
}
